<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=1200, initial-scale=1.0">
    <title>键盘事件</title>
</head>
<body>
    <form action="#">
        <input type="text" id="txtUserName" />
    </form>

    <script>
        const oUserName = document.querySelector('#txtUserName')

        oUserName.addEventListener('keydown',(e) => {
            // console.log('-->keydown')
        })
        oUserName.addEventListener('keypress',(e) => {
            // console.log('-->keypress')
           // console.log(`keypress-keyCode: ${e.keyCode}`)
            // console.log(`keypress-charCode: ${e.charCode}`)
            console.log(`keypress-key:${e.key}`)
            console.log(`keypress-code:${e.code}`)

        })
        oUserName.addEventListener('keyup',(e) => {
            // console.log('-->keyup')
           // console.log(`keyup: ${String.fromCharCode(e.keyCode)}`)
        })
        oUserName.addEventListener('input',(e) => {
            // console.log('-->input')
           // console.log(`input: ${e.keyCode}`)

        })

    </script>
</body>
</html>